﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using KeyCore.Domain.Model;

namespace KeyCore.Framework.DbInit
{
    public class KeyCoreDbModel : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<UserRoles> UsersRoles { get; set; }
    }

    // wykorzystywane do re-kreacji bazy i przy autotestach
    //dodanie konta Admin w grupie administrator(1) z haslem 123456 i mailem
    public class DbSeedingInit : DropCreateDatabaseAlways<KeyCoreDbModel>
    {
        protected override void Seed(KeyCoreDbModel context)
        {
            var newUsersRoles = new List<UserRoles>
                                    {
                                        new UserRoles {RoleName = "administrator"},
                                        new UserRoles {RoleName = "user"}
                                    };

            newUsersRoles.ForEach(x => context.UsersRoles.Add(x));

            var newUser = new List<User>
                              {
                                  new User
                                      {
                                          UserName = "Admin",
                                          Password = "123456",
                                          // [Required(AllowEmptyStrings=true)]
                                          PasswordSalt = "",
                                          Email = "admin@test.pl",
                                          CreatedDate = DateTime.Now,
                                          IsActivated = true, 
                                          UserRole = newUsersRoles.Single(n=>n.RoleName == "administrator")                                                                      
                                      }
                              };

            newUser.ForEach(x => context.Users.Add(x));
            context.SaveChanges();
            base.Seed(context);
        }
    }
}
